/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.confirm;

import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.ConfirmOrderSuccessEvent;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.OmsOrder;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;

import java.util.Date;

import static com.lxk.oms.flow.confirm.FlowConfirmOrderNodeConst.CONFIRM_ORDER_NODE;

/**
 * 确认收货/使用
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(CONFIRM_ORDER_NODE)
public class OrderConfirmOrderNode extends NodeComponent {
    private final IOmsOrderService omsOrderService;
    @Override
    public void process() throws Exception {
        ConfirmOrderContext contextBean = this.getContextBean(ConfirmOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        Long orderId = omsOrderVo.getId();
        OmsOrder update = new OmsOrder();
        update.setReceiveTime(new Date());
        Boolean success = omsOrderService.updateOrderStatusWithInfo(orderId, OrderEnums.OrderStatus.WAIT_CONFIRM_350,
            OrderEnums.OrderStatus.WAIT_EVALUATION_370, update);
        contextBean.setSuccess(success);
    }

    @Override
    public void onSuccess() throws Exception {
        ConfirmOrderContext contextBean = this.getContextBean(ConfirmOrderContext.class);
        if (contextBean.getSuccess()) {
            OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
            if (contextBean.getSendOptionEvent()) {
                // 发送操作事件
                OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
                optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
                optionLogEvent.setUserId(contextBean.getUserId());
                optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
                optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
                optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.CONFIRM_ORDER.formatContent(contextBean.getOrderSn(), contextBean.getUserId().toString()));
                optionLogEvent.setUserType(contextBean.getUserType());
                optionLogEvent.setMerId(omsOrderVo.getMerId());
                optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
                optionLogEvent.setTenantId(contextBean.getTenantId());
                EventHelper.publishEvent(optionLogEvent);
            }
            // 发送成功事件
            ConfirmOrderSuccessEvent successEvent = new ConfirmOrderSuccessEvent();
            successEvent.setOrderNo(omsOrderVo.getOrderNo());
            successEvent.setUserId(contextBean.getUserId());
            successEvent.setTenantId(contextBean.getTenantId());
            EventHelper.publishEvent(successEvent);
        }
    }
}
